package com.xwh.b2c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xwh.b2c.common.PageResult;
import com.xwh.b2c.common.Result;
import com.xwh.b2c.dto.*;
import com.xwh.b2c.entity.Member;
import com.xwh.b2c.service.MemberService;
import com.xwh.b2c.vo.MemberPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author root
 * @since 2025-07-01
 */
@Slf4j
@RestController
@Tag(name = "会员管理")
// 设置请求路径前缀
@RequestMapping("/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "分页查询会员信息", description = "根据条件分页查询会员信息")
    @GetMapping("/page")
    // @ModelAttribute 和 @RequestBody 的区别：
    // @ModelAttribute 接的参数是Get请求?后面的参数
    // @RequestBody 接的参数是 请求体中的 payload（JSON）里的数据
    public PageResult<MemberPageVO> page(@ModelAttribute MemberPageQueryDTO memberPageQueryDTO){
        IPage<MemberPageVO> page = this.memberService.getPage(memberPageQueryDTO);
        return PageResult.success(page);
    }

    @GetMapping("{id}")
    public Result<Member> get(@PathVariable Long id){
        Member member = this.memberService.getById(id);
        return Result.success(member);
    }


    @Operation(summary = "批量删除会员", description = "根据ID列表批量删除会员信息")
    @DeleteMapping()
    public Result<Object> delete(
            @Parameter(description = "会员ID列表",required = true)
            @RequestBody List<Long> ids){
        // memberService父类自带的 removeBatchByIds 物理根据id删除数据
        log.info("ids:{}",ids.toString());
        this.memberService.removeBatchByIds(ids);
        return Result.success();
    }

    @Operation(summary = "编辑会员信息", description = "根据ID编辑会员信息")
    @PutMapping("")
    public Result<Object> update(@RequestBody Member member){
        this.memberService.updateById(member);
        return Result.success();
    }

    // todo 0709 完成启用禁用会员接口
    @Operation(summary = "启用禁用会员", description = "根据ID启用或禁用会员")
    @PutMapping("enable/{id}")
    public Result<Object> enable(@PathVariable Long id){
        Member member = this.memberService.getById(id);
        this.memberService.enable(member);
        return Result.success();
    }
}
